/**
 * @class   wosh::Property
 * @brief   Property is a key-value pair, serialized as strings.
 *          
 *
 *
 ****************************************************************************
 * @version 0.8.499 $Id: Property.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_Property_H__
 #define __WOSH_Core_Property_H__

 #include <woshDefs.h>
 #include <core/Variant.h>
 #include <core/Serialization.h>
 #include <core/Permission.h>


namespace wosh {

template <class T> class DataFieldCollector;


class Property : public ISerializable
 {
	WOSH_SERIALIZABLE(wosh::Property)
	WOSH_COMPARE_RTTI(wosh::Property)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		Property();
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param key [in] the key field (name) of the property
		 */
		explicit Property( const std::string& key );
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param key [in] the key field (name) of the property
		 * @param value [in] the value field (value) of the property
		 */
		explicit Property( const std::string& key, const Variant& value );
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param key [in] the key field (name) of the property
		 * @param value [in] the value field (value) of the property
		 * @param permission [in] the permission field (permission) of the property
		 */
		explicit Property( const std::string& key, const Variant& value, const Permission& permission );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		Property( const Property& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~Property() { }


/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Get the key field (string) of the property
		 * @return returns (constant) STL string reference (to internal field)
		 */
		inline const std::string& getKey() const				{ return this->key; }
		/**
		 * @brief  Get the key field (string) of the property
		 * @return returns (editable) STL string reference (to internal field)
		 */
		inline std::string& getKey()							{ return this->key; }

		/**
		 * @brief  Get the value field (Variant) of the property
		 * @return returns (constant) Variant reference (to internal field)
		 */
		inline const Variant& getValue() const				{ return this->value; }
		/**
		 * @brief  Get the value field (Variant) of the property
		 * @return returns (editable) Variant reference (to internal field)
		 */
		inline Variant& getValue()							{ return this->value; }

		/**
		 * @brief  Evaluate if the propery is empty.
		 * @return true when key and value fields are both empty, false else.
		 */
		inline bool isEmpty() const								{ return (this->key.empty() && this->value.isEmpty() ); }

		/**
		 * @brief  Get the Permission object associated with the property
		 * @return returns (constant) Permission reference
		 */
		inline const Permission& getPermission() const	{ return this->permission; }
		/**
		 * @brief  Get the Permission object associated with the property
		 * @return returns (editable) Permission reference
		 */

		inline Permission& getPermission()				{ return this->permission; }

		static DataFieldCollector<Property>* getDataFields();

		std::string toStringUnixStyle() const;

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Set the key field (string) of the property
		 * @param [in] value the key string to be assigned
		 */
		void setKey( const std::string& value )					{ this->key = value; }

		/**
		 * @brief  Set the value field (as Variant) of the property
		 * @param [in] value the value field to be assigned (copied)
		 */
		void setValue( const Variant& value )				{ this->value = value; }

		/**
		 * @brief  Set the Permission object (as Variant) of the property
		 * @param [in] value the Permission object to be assigned (copied)
		 */
		void setPermission( const Permission& value )		{ this->permission = value; }

		virtual void setPermission( const Permission& value, short overwrite_mode, bool when_empty = false ) {
			this->permission.inherit(value, overwrite_mode, when_empty );
		 }

		virtual void clear();

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual Property& operator=(const Property& m);
		virtual bool operator==(const Property &other) const;
		virtual bool operator!=(const Property &other) const	{ return !(*this == other);  }
//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::string key;		///< key (name) of property
		Variant value;			///< value(content) of property (as string)
		Permission permission;	///< permissions of the object

//@}
	public:
		static const Property EmptyProperty;

}; // class def

}; // namespace wosh

#endif //__WOSH_Core_Property_H__
